<?php
/**
 * Authenticate check for admin area
 *
 * @author     Mario Schillermann <info@rockit-flash.com>
 * @encoding   UTF-8 äöüßÖÄÜ
 * @copyright  © 2011 Mario Schillermann (info@rockit-flash.com)
 * @license    GNU GPL v2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)
 * @category   Module
 * @uses       Zend_Controller_Action
 * @package    Login
 * @subpackage Controller
 * @todo
 */

class Login_LoginController extends Zend_Controller_Action
{
    /**
     * Initialize the general options
     *
     * @return void
     */
    public function init()
    {
        $this->_helper->layout->disableLayout();
    }
    /**
     * Login form
     *
     * @return void
     */
    public function indexAction()
    {
        try {
            $request = $this->_request->getPost();
            
            if($request) {
                
                $auth = new Login_Model_Auth;
                $auth->setUsername($request["username"])
                     ->setPassword($request["password"])
                     ->getAuth();

                if($auth->isVaild()) {

                    $acl = new App_Service_Acl();
                    $acl->setResource(new App_Model_Module());

                    if ($auth->getRoleId() == 1) {
                        $aclObj = $acl->setAdmin();
                    } else {
                        $aclObj = $acl->setAllow(new App_Model_Database_Rules($auth->getRoleId()));
                    }

                    //$acl->setWhitelist(new App_Model_Acl());

                    $zendAuth = Zend_Auth::getInstance();
                    $zendAuth->getStorage()
                         ->write(
                             array(
                                 "username" => $auth->getUsername(),
                                 "userId"   => $auth->getUserId(),
                                 "roleId"   => $auth->getRoleId(),
                                 "rolename" => $auth->getRolename(),
                                 "Zend_Acl" => $aclObj,
                             )
                         );

                    $this->_forward('index', 'dashboard', 'dashboard');
                    
                } else {
                    $this->view->error = "Benutzername oder Passwort falsch!";
                }
            }
        } catch (Zend_Exception $e) {

            error_log(print_r($e, true));
        }
    }
}